/**
 * 
 */
package org.o14x.alpha.ui.commands;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.PlatformUI;
import org.o14x.alpha.domain.ExplorerTab;
import org.o14x.alpha.ui.controller.UIController;

/**
 * Close other tabs command.
 * 
 * @author Olivier DANGREAUX
 */
public class ViewNextTabCommand extends AbstractHandler {
	/**
	 * UIController
	 */
	private UIController uiController;

	/* (non-Javadoc)
	 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
	 */
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		// current ExplorerTab
		ExplorerTab currentExplorerTab = uiController.getDomain().getCurrentExplorerTab();

		// loop over ExplorerTabs
		ExplorerTab[] explorerTabs = uiController.getDomain().getExplorerTabManager().getExplorerTabMap().values().toArray(new ExplorerTab[0]);
		
		int currentExplorerTabIndex = -1;
		for (int i = 0; i < explorerTabs.length && currentExplorerTabIndex == -1; i++) {
			ExplorerTab explorerTab = explorerTabs[i];
			if(explorerTab == currentExplorerTab) {
				currentExplorerTabIndex = i;
			}			
		}
		
		if(currentExplorerTabIndex < explorerTabs.length -1) {
			currentExplorerTabIndex++;
		}
		else {
			currentExplorerTabIndex = 0;
		}
		
		PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().activate(explorerTabs[currentExplorerTabIndex].getFolderView());
		
		return null;
	}

	/**
	 * Returns the value of uiController.
	 *
	 * @return The value of uiController.
	 */
	public UIController getUiController() {
		return uiController;
	}

	/**
	 * Sets the value of uiController.
	 *
	 * @param uiController The value of uiController to set.
	 */
	public void setUiController(UIController uiController) {
		this.uiController = uiController;
	}

}
